﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace MyCollections
{
    public partial class DetailsMovie : Form
    {
        private int index;

        public DetailsMovie()
        {
            InitializeComponent();
        }

        public DetailsMovie(int index)
        {
            InitializeComponent();
            this.index = index;
            load();
            setAllReadOnly(true);
            bt_accept.Enabled = false;


            

        }

        private void load()
        {
            // FIXME: get pathxml from config
            string pathxml = "XMLData\\db_movies.xml";

            XmlDocument xmldoc = getXML(pathxml);
            XmlNamespaceManager xmlns = new XmlNamespaceManager(xmldoc.NameTable);
            XmlNodeList xmlnodelist = xmldoc.SelectNodes("/dataroot/Filmes[ID="+this.index+"]/*", xmlns);
            
            loadCombos();

            this.id.Text = xmlnodelist[0].InnerText;
            this.title.Text = xmlnodelist[1].InnerText;
            this.genre.Text = xmlnodelist[2].InnerText;
            this.format.Text = xmlnodelist[3].InnerText;
            this.date.Text = xmlnodelist[6].InnerText;

        }

        private void loadCombos()
        {
            string pathxml = "XMLData\\movies.xml";

            XmlDocument xmldoc = getXML(pathxml);
            XmlNamespaceManager xmlns = new XmlNamespaceManager(xmldoc.NameTable);
            XmlNodeList xmlnodelist = xmldoc.SelectNodes("/Movies/type/*", xmlns);

            // Load ComboBox TYPE
            string[] array = new string[xmlnodelist.Count];
            for (int i = 0; i < xmlnodelist.Count; i++)
            {
                array[i] = xmlnodelist[i].InnerText;
            }
            format.Items.AddRange(array);

            xmlnodelist = xmldoc.SelectNodes("/Movies/format/*", xmlns);

            // Load ComboBox FORMAT
            array = new string[xmlnodelist.Count];
            for (int i = 0; i < xmlnodelist.Count; i++)
            {
                array[i] = xmlnodelist[i].InnerText;
            }
            genre.Items.AddRange(array);
        }

        private void setAllReadOnly(bool value)
        {
            id.Enabled = !value;
            title.Enabled = !value;
            format.Enabled = !value;
            genre.Enabled = !value;
            date.Enabled = !value;
        }

        private XmlDocument getXML(string filepath)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(filepath);
            return doc;
        }

        private void bt_edit_Click(object sender, EventArgs e)
        {
            setAllReadOnly(false);
            bt_accept.Enabled = true;
        }

        private void bt_cancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void bt_info_Click(object sender, EventArgs e)
        {
            webbrowser obj = new webbrowser();
            obj.MdiParent = this.MdiParent;
            obj.Show();
        }
    }
}
